package com.woniuxy.springmvcparam.interceptor;

import com.woniuxy.springmvcparam.bean.LoginLogBean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author linke
 * @version 1.0
 * @className: LoginLogInterceptor
 * @description: 实现HandlerTnterceptor接口的拦截器
 * @date 2021/11/14 10:55
 */
@Component
public class LoginLogInterceptor implements HandlerInterceptor {
    /**
     * 执行在Controller方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("我是登录拦截器，我执行在LoginController方法之前！！！");

        //表示Controller 中的方法，可以执行
        //return true;
        //表示Controller 中的方法，不需要执行
        return false;
    }

    /**
     * 执行在Controller方法执行之后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("我是登录拦截器，我执行在LoginController方法之后！！！");
        String ip = getIpAddress(request);
        //记录登录日志
        LoginLogBean loginLogBean =
                new LoginLogBean(null,"李寻欢","xiaoli",ip,new Date());
        //调用业务层的方法，完成登录日志的保存
        System.out.println(loginLogBean);
    }

    /**
     * 执行在所有的拦截器 的preHandle() 和postHandle() 执行完毕之后的收尾的方法
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("我是登录拦截器，我是一个收尾的方法！！！");
    }


     /** 获得IP地址
     * @param request
     * @return
             */
    public String getIpAddress(HttpServletRequest request){
        String ip = null;

        //X-Forwarded-For：Squid 服务代理
        String ipAddresses = request.getHeader("X-Forwarded-For");

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //Proxy-Client-IP：apache 服务代理
            ipAddresses = request.getHeader("Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //WL-Proxy-Client-IP：weblogic 服务代理
            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //HTTP_CLIENT_IP：有些代理服务器
            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //X-Real-IP：nginx服务代理
            ipAddresses = request.getHeader("X-Real-IP");
        }

        //有些网络通过多层代理，那么获取到的ip就会有多个，一般都是通过逗号（,）分割开来，并且第一个ip为客户端的真实IP
        if (ipAddresses != null && ipAddresses.length() != 0) {
            ip = ipAddresses.split(",")[0];
        }

        //还是不能获取到，最后再通过request.getRemoteAddr();获取
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
